﻿// -----------------------------------------------------------------------
// <copyright file="ConfigCookieEntry.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace NetFramework.Web.Configuration
{
    using System;
    using System.Xml.Serialization;

    [Serializable]
    public class ConfigCookieEntry
    {
        private string name;
        private string subName;
        private bool topLevelDomain;
        private string path = "/";
        private bool renderInClient = false;
        private bool secureOnly = false;
        private TimeSpan expiresAfter;

        /// <summary>
        /// Gets or sets the name of a cookie.
        /// </summary>
        [XmlAttribute("name")]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// Gets or sets the sub name of a cookie.
        /// </summary>
        [XmlIgnore]
        public string SubName
        {
            get { return subName; }
            set { subName = value; }
        }

        /// <summary>
        /// Gets or sets the domain to associate the cookie with.
        /// </summary>
        /// <value>The name of the domain to associate the cookie with. The default value is the current domain. </value>
        [XmlAttribute("topLevelDomain")]
        public bool TopLevelDomain
        {
            get { return topLevelDomain; }
            set { topLevelDomain = value; }
        }

        /// <summary>
        /// Gets or sets the virtual path to transmit with the current cookie.
        /// </summary>
        [XmlAttribute("path")]
        public string Path
        {
            get { return path; }
            set { path = value; }
        }

        /// <summary>
        /// GGets or sets the expiration for the cookie.
        /// If the cookie never expires, the ExpiresAfter.Ticks = 0.
        /// </summary>        
        [XmlIgnore]
        public TimeSpan ExpiresAfter
        {
            get { return expiresAfter; }
            set { }
        }

        /// <summary>
        /// 
        /// </summary>
        [XmlAttribute("expiresAfter")]
        public string ExpiresAfterInner
        {
            get { return null; }
            set { expiresAfter = TimeSpan.Parse(value); }
        }

        /// <summary>
        /// Cookie有效期，以秒计算
        /// </summary>
        [XmlIgnore]
        public double Expires
        {
            get { return ExpiresAfter.TotalSeconds; }
            set { }
        }

        /// <summary>
        /// Gets or sets a value indicating whether to transmit the cookie using Secure Sockets Layer (SSL)--that is, over HTTPS only. 
        /// </summary>
        /// <value><c>true</c> to transmit the cookie over an SSL connection (HTTPS); otherwise, <c>false</c>. The default value is <c>false</c>. </value>
        [XmlAttribute("secureOnly")]
        public bool SecureOnly
        {
            get { return secureOnly; }
            set { secureOnly = value; }
        }

        /// <summary>
        /// Gets or sets a value that specifies whether a cookie is accessible by client-side script.  
        /// </summary>
        /// <value><c>true</c> if the cookie has the HttpOnly attribute and cannot be accessed through a client-side script; otherwise, <c>false</c>. The default is <c>false</c>. </value>
        [XmlAttribute("renderInClient")]
        public bool RenderInClient
        {
            get { return renderInClient; }
            set { renderInClient = value; }
        }

        /// <summary>
        /// Gets a value indicating whether expiration is enabled for the current cookie.
        /// </summary>
        /// <value><c>true</c> if expiration is enabled; otherwise, <c>false</c>.</value>
        [XmlIgnore]
        public bool EnableExpiration
        {
            get { return ExpiresAfter.Ticks != 0; }
        }
    }    
}
